package com.itheima.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Icon;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findall")
    public Result findAll(String username){
        try {
            List<Menu> menu = menuService.findByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findallmenu")
    public Result findAllMenu(){
        try {
            List<Menu> menu = menuService.findAll();
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findpage")
    public PageInfo pageQuery(@RequestBody QueryPageBean queryString){
        PageInfo info = menuService.pageQuery(queryString);
        return info;
    }

    @RequestMapping("/findlevel1menu")
    public Result findLevel1Menu(){
        try {
            List list = menuService.findLevel1Menu();
            return new Result(true,MessageConstant.GET_MENU_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_MENU_LIST_FAIL);
        }
    }

    @RequestMapping("/findlevel1menuwithoutself")
    public Result findlevel1menuwithoutself(Integer id){
        try {
            List<Menu> list = menuService.findLevel1MenuWithoutSelf(id);
            return new Result(true,MessageConstant.GET_MENU_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_MENU_LIST_FAIL);
        }
    }

    @RequestMapping("/findicons")
    public Result findIcons(){
        try {
            List<Icon> list = menuService.findIcons();
            return new Result(true,MessageConstant.GET_ICON_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_ICON_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_ICON_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.ADD_ICON_FAIL);
        }
    }

    @RequestMapping("/findbyid")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            List<Menu> children = menuService.findChildren(menu.getId());
            Integer parentMenuId = menu.getParentMenuId();
            if (children.size() != 0 && parentMenuId != null){
                return new Result(false,"菜单不能超过2级");
            }
            menuService.update(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @RequestMapping("/deletebyid")
    public Result DeleteById(Integer id){
        try {
            menuService.DeleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
